
/**
 * 保额、保费选择下拉和输入框时，option更改对应的默认值
 */

import { generateUID } from '@/utils'

const defaultOption = (optionType = '1') => [
  optionType === '1' ? {
    'optionName': '',
    'optionValue': '',
    'defaultFlag': '',
    'uid': generateUID(),
    'rule': []
  } : {
    'optionValue': '',
    'optionMin': '',
    'optionMax': '',
    'uid': generateUID(),
    'rule': []
  }
]
const state = {
  // 配置项的所有数据集合
  premiumCalculationConfig: null,
  // 选中的配置项
  activeCfgType: null,
  // 投保人内容项
  applicant: [],
  // 被保人内容项
  insured: [],
  // 保险详情内容项
  insuranceDetail: [],
  // 选中限制条件的uid(配置项里面)
  restrictionsUid: null,
  // 选中选项配置默认的值uid
  // optionActiveUid: null
}

const getters = {
  // 配置项的json数据
  // premiumCalculationConfig: state => {
  //   return {
  //     applicant: state.applicant,
  //     insured: state.insured
  //   }
  // },
  // 选中(投保人 || 被保人 || 报价条件)的数据
  activeItemData: state => {
    const { insuranceDetail, activeCfgType, applicant, insured } = state
    return insuranceDetail?.find(item => item.optionField === activeCfgType) || applicant?.find(item => item.optionField === activeCfgType) || insured?.find(item => item.optionField === activeCfgType)
  },
  // 选中限制条件的数据
  restrictions: (state, getters) => {
    const { restrictionsUid } = state
    const { activeItemData } = getters
    return activeItemData?.option?.find(item => item.uid === restrictionsUid)
  },
  // 获取当前报价条件前面的数据
  getActionPreData(state) {
    const { activeCfgType, applicant, insured, insuranceDetail } = state
    const activeIndex = insuranceDetail.findIndex(item => item.optionField === activeCfgType)
    return [
      ...applicant,
      ...insured,
      ...insuranceDetail.slice(0, activeIndex)
    ]
  }
}


const mutations = {
  SETPREMIUMCALCULATIONCONFIG: (state, premiumCalculationConfig) => {
    state.premiumCalculationConfig = premiumCalculationConfig
    state.insuranceDetail = premiumCalculationConfig?.filter(item => !item.optionField.includes('age') && !item.optionField.includes('sex'))
    state.applicant = premiumCalculationConfig?.filter(item => item.optionField === 'applicant.sex' || item.optionField === 'applicant.age')
    state.insured = premiumCalculationConfig?.filter(item => item.optionField === 'insured.sex' || item.optionField === 'insured.age')
    // premiumCalculationConfig.forEach(item => {
    //   if (item.optionField === 'applicant.sex' || item.optionField === 'applicant.age') {
    //     // state.applicant.push(item)
    //   } else if (item.optionField === 'insured.sex' || item.optionField === 'insured.age') {
    //     // state.insured.push(item)
    //   } else {
    //     state.insuranceDetail.push(item)
    //   }
    // })
  },
  // 设置投保人
  SETAPPLICANT: (state, applicant) => {
    state.applicant = applicant
  },
  // 设置被保人
  SETINSURED: (state, insured) => {
    state.insured = insured
  },
  SETACTIVECFGTYPE: (state, activeCfgType) => {
    state.activeCfgType = activeCfgType
  },
  // 新增报价条件
  ADDINSURANCEDETAIL: (state, insuranceDetail) => {
    state.insuranceDetail.push(insuranceDetail)
  },
  // 删除报价条件
  DELINSURANCEDETAIL: (state, index) => {
    state.insuranceDetail.splice(index, 1)
  },
  // 设置选中项的optionType
  SETOPTIONTYPE(state, val) {
    const { insuranceDetail, activeCfgType } = state
    insuranceDetail.forEach(item => {
      if (item.optionField === activeCfgType) {
        item.optionType = val
        item.option = defaultOption(item.optionType)
        item.defaultValue = ''
      }
    })
  },
  // 设置选中限制条件的uid
  SETRESTRICTIONSUID(state, restrictionsUid) {
    state.restrictionsUid = restrictionsUid
  },
  // 清空选中限制条件的uid
  CLEARRESTRICTIONSUID(state) {
    state.restrictionsUid = null
  },
  // 设置配置项option的默认选中值

  // SETOPTIONACTIVEUID(state, optionActiveUid) {
  //   state.optionActiveUid = optionActiveUid
  // },

  // 重新设置insuranceDetail里面的某条数据
  SETITEMDATA(state, data) {
    const { insuranceDetail, applicant, insured } = state
    const { optionField, optionType } = data
    if (optionField === 'applicant.age' || optionField === 'applicant.sex') {
      // 投保人
      const index = applicant?.findIndex(item => item.optionField === optionField && item.optionType === optionType)
      state.applicant.splice(index, 1, data)
    } else if (optionField === 'insured.age' || optionField === 'insured.sex') {
      // 被保人
      const index = insured?.findIndex(item => item.optionField === optionField && item.optionType === optionType)
      state.insured.splice(index, 1, data)
    } else {
      // 报价条件
      const index = insuranceDetail?.findIndex(item => item.optionField === optionField && item.optionType === optionType)
      state.insuranceDetail.splice(index, 1, data)
    }
  },

  // 对应的item设置数据

}

const actions = {

}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
